<?php

namespace Util;




/*
签名串排序
*/
function ASCIIAZ($arrayData)
{

    ksort($arrayData);
    // 将数组转换为 URL 编码的字符串
    $data1 = http_build_query($arrayData);
    $data2 = urldecode($data1);
    return $data2;
}




/*
RSA2签名方法
*/
function RSA2sign($signatureContent)
{

    /*
商户私钥
*/
    $private_key_str =
        "-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCDsXjvolWNgNMlJlrvFcCq9pYPLk9nIFZF3HnG4iTycgLYe3Un6fpgMeHXzhC9hz9bHIrUJQ16vetxoUSb9lW0SdMtteEiJtGiNebT7P1R5ScOja5RYpd8qdt1ukTGJmoP4+zUQ8/jxpq4oymrX6rA+OgIFVmaXigAh9J1MxLIWpliIieUejdAhKBTEfSCMjfPROj9UoFoKl6qKRdv07r1kyfpWqUXjHkzX0ax/B9sP0fhyPwQHJxkClK/MnwIPUKLkWop4aNlW+Qr5NSdCN5D1Degl2uBdi1tUrlqjHnxHCHdSF6cQ9ZUTdt3aEShf69OgpRe+YtYrrpQx4gpwEuXAgMBAAECggEAbn5aVN1kYLyE6/eZdl5O8GLCluwpLQKhwC3zct5NV6o3HwG+04VqUImpztKTkyHGE6EIDmUkQSbWQ9mmYiE12XXTnNeslGXzn+EP0iJWjP7yXTCc4KYTKkKt4sItb//NH5YCIDrhU2BQcrhbfvFeqFhu/lFDg9SaRY+wEiuPB9BXaTRM8H6YAohG64B3reYoK/65bonux0TX9RwVEfEfa2NiX8tqCZcTRaNPaoKVTOGUJsnSzcK3Xc9s6dm2I2GejEGZPjZwNXy9GA6QEp9NP6YS/ifEIPP0DBUpKjK457ymJx+lYuMy+Pjj//ENpmcW5HGNbzpAeM5DIrlWLdDtmQKBgQDozpk6Pu1xkr4y7zRwYILfl64k1lVPCoDFXR/hfJw6LQQe8aQMxvyn6gzH87BQIDh5jTOxHreuVeikJKqZPEUq8R9bU/81jXXYN80+K2kX9i36/mMA9ngLgCPPD5q+Rpdhfp7+SDU+Hr5tKLdzMMBsTsIRBDrp32iz4dFJegWl9QKBgQCQ0B2QBHIw1SMjQmAcXpdCt3dOWDiG3WpJIFKuP0ekewUVrm4CdD8tf1pbtK6mZk25q+2IeVFtQ3x6TQLqg/AmZkdBmy7UtwCHqz4ANI3klEdWfD+Wohpa5I9etk6kAjR/eJvFJgYfN63iM38vqfYo4z+Bdx4cMj+MpX71Vzcn2wKBgQCod8C4PotS0Y7jiugWnjB2nuu86oEnRoFy0PXeFW90Rdkij1hYirKQzDTdiQHAqeBUTZLnjEP9W1uTVJlDdDLVZVF8pGcdGLX7dj4GzCEr3tDdPbLlInYyVFMVZsRjBvBK8/cYKaalwleo5HsEV3YXqlKTP81mKm45JaNumWd29QKBgEumvVroR7NlFTctTMJlVAYBjG08P0ONViIfpbHr0wSFU/Gwrz3O9tZQ6qZv7LF3TnGJ9MxP/I7lK5lEtBpJlOKqmd0XMc7kT32QoxI+YkYghGgU/q81vijvsEpdWRXrJPvWySGkLJyQXOGH35DJ7JQNq7Z5wRwfz/0s0/ddea3zAoGASWW0Uo1Lk10ejbDwdU0KqKpsvvKDeaKEhoyhmilg288MvT8oqXuSfRiHdJ+6TJL+jcdNTUOcew8V2ymtoH5UhdYVfMSb5okqY17GzMK3dTbZSEg5j0k155EUzdYU7tGAwAvDIxXJpb/9fM0tdctoWbwd0/7Xy4JqsRmLs4Zv5tE=
-----END PRIVATE KEY-----";
    $private_key = openssl_pkey_get_private($private_key_str);
    openssl_sign($signatureContent, $signature, $private_key, OPENSSL_ALGO_SHA256);
    $sign = base64_encode($signature);
    //输出签名后数据
    return $sign;
}



function PostJson($Posturl,$jsonData)
{
// 目标URL
    $url = $Posturl;


// 创建Header选项
    $options = array(
        'http' => array(
            'header'  => "Content-type: application/json\r\n",
            'method'  => 'POST',
            'content' => $jsonData,
        ),
    );

// 创建上下文选项
    $context  = stream_context_create($options);

// 发送POST请求并获取响应

    $response = file_get_contents($url, false, $context);

    if ($response === false) {
        // 处理请求失败的情况
        return "Failed to send POST request.";
    } else {
        // 处理请求成功的响应
        return $response;
        return "<br/><hr>";
    }
}

/*
Post+form-data请求，文件上传专用
*/
function Postformdata($url, $data) {

    //初使化init方法
    $ch = curl_init();

    //指定URL
    curl_setopt($ch, CURLOPT_URL, $url);

    //设定请求后返回结果
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    //声明使用POST方式来进行发送
    curl_setopt($ch, CURLOPT_POST, 1);

    //发送什么数据呢
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);


    //忽略证书
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

    //忽略header头信息
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

    //设置超时时间
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);

    //发送请求
    $output = curl_exec($ch);

    //关闭curl
    curl_close($ch);

    //返回数据
    return $output ;
}


/*
图片转换为二进制
*/
function PicToTow($img)
{
    $fp   = fopen($img, 'rb'); // 打开图片
    $content = fread($fp, filesize($img));//二进制数据
    $md1=md5($content);
    return $md1; //输出二进制数据
}

/*
删除json指定键的值双引号
*/
function DeleteValue($jsonString)
{

    // 将 JSON 字符串解码为关联数组
    $arrayData = json_decode($jsonString, true);

// 处理 biz_content 键的值，去掉外层双引号
    if (isset($arrayData['biz_content'])) {
        $arrayData['biz_content'] = json_decode($arrayData['biz_content'], true);
    }

// 打印结果
    $arrayData1 = json_encode($arrayData,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);

    return $arrayData1;

}

//验签方法

function verifysign($dataString,$signString)
{
//$dataString 被签名数据
//$signString 已经签名的字符串
    $partner_public_key_str = "-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjsr1LH1t0JbdYGjYNnA
CdLEX5H44cstATVJKZgqDi0h3MV0cPdNA4bENVtDvGpo6hh/tGI780OuLjBorev
1/MvDaBEQPnqizm+xKQs0crtDhmH0CyWPKLAHTfJY/K0aTWcLEKvWnNtrghYps+
CuH1S3+qXiB51FLlyxm7yPlNhe6gs4f2TwLfwMtwvV2CfWTD9d5u2XxLHJURtdx
VOnk1x8jUj3AMgt0iGXktjMGXe/wMlDI7apAJ9escGPTb+RCnREgbumhjxLUJ2q
kRjn0n011QRz56vO4UGrp9UBDcdXGUgUaxZ89mGm58YAQ071y1GaACcXrUs/73h
pleBjyFwIDAQAB
-----END PUBLIC KEY-----";
    $signature = base64_decode($signString);
    $flg = openssl_verify($dataString, $signature, $partner_public_key_str, OPENSSL_ALGO_SHA256);
    if ($flg) {//验签成功
        echo '验签成功';
    } else {
        echo "验签失败";
    }
}


?>